﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DOTNETGROUP.Data;
using DOTNETGROUP.Entities;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

public partial class Chi_Tiet_Tin_Tuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string idTintuc = Request.Params["Id"];
            HienThi_ChitietTinTuc(Convert.ToInt32(idTintuc));
        }
    }

    protected void HienThi_ChitietTinTuc(int idTintuc)
    {
        // Nhan ve id va tieu de cua tin tuc
        TinTuc entTinTuc = DataRepository.TinTucProvider.GetById(idTintuc);
        int _view = 0;
        // Kiem tra xem du lieu (doi tuong) lay duoc co bi null khong
        if (entTinTuc != null)
        {
            string WhereClause = "IdLoaiTinTuc= " + entTinTuc.IdLoaiTinTuc + "and Id<>" + idTintuc;
            DataSet dsTinTuc = DataRepository.Provider.ExecuteDataSet("TinTuc_GetPaged", WhereClause, "NgayCapNhat DESC", 0,0);
            dtlDsTinTuc.DataSource = dsTinTuc;
            dtlDsTinTuc.DataBind();
            // Hien thi du lieu hien tai cua tin tuc do
            lblTieude.Text = entTinTuc.TieuDe;
            
            lblNgaydang.Text = entTinTuc.NgayCapNhat.ToShortDateString();
            lblNguoidang.Text = GetNguoiDung(entTinTuc.IdNguoiDung.ToString());
            lblNoidung.Text = Server.HtmlDecode(entTinTuc.NoiDung);
            litTieuDe.Text = entTinTuc.TieuDe;
            litDienGiai.Text = String.Format("<meta content='{0}' name='description' />", entTinTuc.TomTat);
            _view += entTinTuc.LuotXem;
            _view += 1;

            string query = string.Empty;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
            conn.Open();
            query = "UPDATE TinTuc SET LuotXem = '" + _view + "' where Id = '" + idTintuc + "'";
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = conn;
            sqlCommand.CommandText = query;
            sqlCommand.ExecuteNonQuery();
            conn.Close();

            lblLuotxem.Text = entTinTuc.LuotXem.ToString(CultureInfo.InvariantCulture);
        }               

        // Cap nhat luot xem cua Tin tuc

    }

    public string GetNguoiDung(object id)
    {
        int s = Convert.ToInt32(id.ToString());
        NguoiDung ent_NguoiDung = DataRepository.NguoiDungProvider.GetById(s);
        return ent_NguoiDung.HoTen;
    }

    /// <summary>
    /// Phuong thuc chuyen tieu de co dau thanh khong dau de dua vao link
    /// </summary>
    /// <param name="Char"></param>
    /// <returns></returns>
    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
    /// <summary>
    /// Format cat chuoi
    /// </summary>
    /// <param name="str"></param>
    /// <param name="n"></param>
    /// <returns></returns>
    public string formatString(string str, int n)
    {
        string kq = "";

        if (str.Length > n)
            kq = str.Substring(0, n + 1) + "...";
        else
            kq = str;

        return kq;
    }

}